#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //    ui->msg_lable->adjustSize();
    setting=new QSettings(tr("msc"),tr("Upgrade"));
    QThread *th = new QThread;
    serial.moveToThread(th);
    th->start();

    QThread *thsendfile=new QThread;
    filesend.moveToThread(thsendfile);
    thsendfile->start();

    ui->progressBar->setMinimum(0);

    connect(&serial,&myserial::disPortInfo,this,&MainWindow::dis_available_port);
    connect(this,&MainWindow::getavaliableport,&serial,&myserial::get_available_port);
    connect(this,&MainWindow::open_serial_port,&serial,&myserial::openport);
    connect(this,&MainWindow::tc,&serial,&myserial::tuichu);
    connect(&serial,&myserial::change_pbopen,ui->PB_Open,&QPushButton::setText);
    connect(this,&MainWindow::setsendfilename,&filesend,&sendfile::setfilename);
    connect(&filesend,&sendfile::sendmsg,&serial,&myserial::writedata);
    connect(ui->PB_Upgrade,&QPushButton::clicked,&filesend,&sendfile::upgrade_frameware);
    connect(&serial,&myserial::dismsg,this,&MainWindow::showmsg);
    connect(&timscan,&QTimer::timeout,this,&MainWindow::prameter_init);
    connect(&filesend,&sendfile::dis_result,this,&MainWindow::showmsg);
    connect(&serial,&myserial::err,this,&MainWindow::showwarning);
    connect(&filesend,&sendfile::err,this,&MainWindow::showwarning);
    connect(&filesend,&sendfile::setProgress,ui->progressBar,&QProgressBar::setValue);
    connect(&filesend,&sendfile::setProgressMax,ui->progressBar,&QProgressBar::setMaximum);
    connect(ui->actionexit,&QAction::triggered,this,&MainWindow::close);
    connect(ui->actionAboutQT,&QAction::triggered,this,&MainWindow::showaboutqt);
    connect(ui->lineEdit,&QLineEdit::textChanged,&filesend,&sendfile::setfilename);
    timscan.setSingleShot(true);
    timscan.start(100);

}

MainWindow::~MainWindow()
{
    emit tc();
    QThread::msleep(10);
    delete ui;
}


void MainWindow::showaboutqt()
{
    QMessageBox::aboutQt(this,"AboutQT");
}
void MainWindow::prameter_init()
{
    QString path=setting->value(tr("filepath")).toString();
    if(!path.isEmpty())
    {
        ui->lineEdit->setText(path);
        emit setsendfilename(path);
    }
    emit getavaliableport();
}


void MainWindow::dis_available_port(QList<QSerialPortInfo> info)
{
    foreach (auto port, info) {
        ui->comboBox->insertItem(QComboBox::InsertAfterCurrent,port.portName());
    }
}

void MainWindow::on_PB_Open_clicked()
{
    emit open_serial_port(ui->comboBox->currentText());

}

void MainWindow::on_PB_Select_clicked()
{
    QString tmpname=QFileDialog::getOpenFileName(this,
                                                 tr("select bin file to update!"),
                                                 setting->value(tr("filepath"),QStandardPaths::standardLocations(QStandardPaths::DesktopLocation).at(0)).toString(),
                                                 tr("bin(*.bin)"));
    if(tmpname.isEmpty())
    {
        QMessageBox::warning(this,tr("err"),tr("please select valid file path!"),QMessageBox::Ok);
    }
    emit setsendfilename(tmpname);
    setting->setValue(tr("filepath"),tmpname);
    ui->lineEdit->setText(tmpname);
}

void MainWindow::showmsg(QString str)
{
    ui->msg_lable->setText(str);
}

void MainWindow::on_PB_Scan_clicked()
{
    ui->comboBox->clear();
    emit getavaliableport();
}

void MainWindow::showwarning(QString msg)
{
    QMessageBox::warning(this,tr("warning"),msg,QMessageBox::Ok);
}
